package vaccine.appointment.system.vaccinationsite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vaccine.appointment.system.common.enums.DelFlgEnum;
import vaccine.appointment.system.common.errorcode.CommonErrorCode;
import vaccine.appointment.system.common.exception.CommonException;
import vaccine.appointment.system.vaccinationsite.dataobject.VaccinationSite;
import vaccine.appointment.system.vaccinationsite.mapper.VaccinationSiteMapper;
import vaccine.appointment.system.vaccinationsite.service.VaccinationSiteService;
import vaccine.appointment.system.vaccinationsite.vo.AddVaccinationSiteRequest;
import vaccine.appointment.system.vaccinationsite.vo.UpdateVaccinationSiteRequest;
import vaccine.appointment.system.vaccinationsite.vo.VaccinationSiteVO;

import java.util.ArrayList;
import java.util.List;
import vaccine.appointment.system.vaccine.dataobject.Vaccine;

@Service
public class VaccinationSiteServiceImpl implements VaccinationSiteService {
    @Autowired
    private VaccinationSiteMapper vaccinationSiteMapper;

    @Override
    public VaccinationSite selectVaccinationSiteById(Integer id) {
        return vaccinationSiteMapper.selectById(id);
    }

    @Override
    public Boolean add(VaccinationSite vaccinationSite) {
        vaccinationSiteMapper.insert(vaccinationSite);
        return true;
    }

    @Override
    public Boolean delete(Integer id) {
        VaccinationSite vaccinationSite = vaccinationSiteMapper.selectById(id);
        vaccinationSiteMapper.updateById(vaccinationSite);
        return true;
    }

    @Override
    public Boolean update(VaccinationSite vaccinationSite) {
        vaccinationSiteMapper.updateById(vaccinationSite);
        return true;
    }

    /**
     * 查询所有接种点
     */
    @Override
    public List<VaccinationSiteVO> getAllSites() {
        QueryWrapper<VaccinationSite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flg", DelFlgEnum.NOT_DELETED.getCode());
        queryWrapper.orderByDesc("id");

        List<VaccinationSite> siteList = vaccinationSiteMapper.selectList(queryWrapper);

        List<VaccinationSiteVO> voList = new ArrayList<>();
        for (VaccinationSite site : siteList) {
            VaccinationSiteVO vo = new VaccinationSiteVO();
            vo.setId(site.getId());
            vo.setName(site.getName());
            vo.setAddress(site.getAddress());
            vo.setPhoneNum(site.getPhoneNum());
            voList.add(vo);
        }

        return voList;
    }

    /**
     * 添加接种点（管理员功能）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addSite(AddVaccinationSiteRequest request) {
        VaccinationSite site = new VaccinationSite();
        site.setName(request.getName());
        site.setAddress(request.getAddress());
        site.setPhoneNum(request.getPhoneNum());
        site.setDelFlg(DelFlgEnum.NOT_DELETED.getCode());

        vaccinationSiteMapper.insert(site);
        return true;
    }

    /**
     * 更新接种点（管理员功能）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateSite(Integer siteId, UpdateVaccinationSiteRequest request) {
        VaccinationSite site = vaccinationSiteMapper.selectById(siteId);
        if (site == null || site.getDelFlg().equals(DelFlgEnum.DELETED.getCode())) {
            throw new CommonException(CommonErrorCode.NOT_FOUND);
        }

        site.setName(request.getName());
        site.setAddress(request.getAddress());
        site.setPhoneNum(request.getPhoneNum());

        vaccinationSiteMapper.updateById(site);
        return true;
    }

    /**
     * 删除接种点（管理员功能）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteSite(Integer siteId) {
        VaccinationSite site = vaccinationSiteMapper.selectById(siteId);
        if (site == null || site.getDelFlg().equals(DelFlgEnum.DELETED.getCode())) {
            throw new CommonException(CommonErrorCode.NOT_FOUND);
        }

        site.setDelFlg(DelFlgEnum.DELETED.getCode());
        vaccinationSiteMapper.updateById(site);
        return true;
    }
}
